package cn.com.api.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.com.api.entity.ApiT_order_detail;
import cn.com.api.service.ApiT_order_detailService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "t_order_detail/")
public class ApiT_order_detailController {
	@Autowired
	private ApiT_order_detailService apiT_order_detailService;

	private static Logger logger = Logger.getLogger(ApiT_order_detailController.class);

	/**
	 * 创建订单
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "create_order_detail")
	public void create_order_detail(HttpServletRequest request, HttpServletResponse response,
			ApiT_order_detail t_order_detail) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// apiT_order_detailService.insertT_order_detail(t_order_detail);
			// return
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "添加成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_order_detailById")
	public void findT_order_detailById(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiT_order_detail t_order_detail = apiT_order_detailService.findT_order_detailById(Integer.valueOf(id));

			// return
			map.put("datas", t_order_detail);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	/**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_order_details")
	public void findT_order_details(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<ApiT_order_detail> t_order_details = apiT_order_detailService.findT_order_details();

			// return
			map.put("datas", t_order_details);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_order_detail")
	public void updateT_order_detail(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String did = request.getParameter("did");
		String rid = request.getParameter("rid");
		String money = request.getParameter("money");
		String num = request.getParameter("num");
		String product_id = request.getParameter("product_id");
		String id = request.getParameter("id");
		String points = request.getParameter("points");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");

		// String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {
				ApiT_order_detail t_order_detail = new ApiT_order_detail();
				// 获取原数据
				t_order_detail = apiT_order_detailService.findT_order_detailById(Integer.valueOf(id));
				// 修改
				// t_order_detail.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiT_order_detailService.updateT_order_detail(t_order_detail);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteT_order_detail")
	public void deleteT_order_detail(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {

				Integer ret_I = apiT_order_detailService.deleteT_order_detail(Integer.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
